//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.Research.DataLayer
{
    public partial class DataProductBrowser : Form
    {
        private Connection conn;
        private DataProduct selected;

        internal DataProductBrowser()
        {
            InitializeComponent();
        }


        public DataProduct Selected
        {
            get
            {
                return selected;
            }
        }

        public DataProductBrowser(Control parent, Connection conn)
        {
            this.conn = conn;

            InitializeComponent();

            if (parent != null)
            {
                this.Parent = parent;
                this.StartPosition = FormStartPosition.CenterParent;
            }

            this.searchResults.Items.Clear();
            selectBtn.Enabled = (searchResults.SelectedItems.Count > 0);
            searchResultsStatusText.Text = "";
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            searchResultsStatusText.Text = string.Format("Searching...");
            
            List<DataProduct> dpFound = DataProduct.Search(searchText.Text, conn);
            LoadDataProducts(dpFound);

            //----------------------------------------------
            // Visual enhancements
            searchResultsStatusText.Text = string.Format("Found {0} items that match the criteria",
                searchResults.Items.Count);

            if (searchResults.Items.Count == 0)
            {
                // No items found. Display something nice to the user
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems.Add("Search found no items");
                searchResults.Items.Add(lvi);
                searchResults.Enabled = false;
            }
            else
            {
                // We have results. Allow user to interact with the list
                searchResults.Enabled = true;
            }
        }

        private void LoadDataProducts(List<DataProduct> dpList)
        {
            this.searchResults.Items.Clear();
            foreach (DataProduct dp in dpList)
            {
                AddSearchResults(dp, 0);
            }
        }

        private void searchResults_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectBtn.Enabled = (searchResults.SelectedItems.Count > 0);
            selected = (searchResults.SelectedItems.Count == 0 ? null : searchResults.SelectedItems[0].Tag as DataProduct);
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            selected = null;
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void selectBtn_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();            
        }

        private void AddSearchResults(DataProduct obj, int icon)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.SubItems.Add(obj.Name);
            lvi.SubItems.Add(obj.Type);
            lvi.ImageIndex = icon;
            lvi.Tag = obj;
            searchResults.Items.Add(lvi);
        }

        private void searchText_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        Search();
                    }));
                    e.SuppressKeyPress = true;
                    break;
            }
        }
    }
}
